package leetcode;

/*
404. 左叶子之和
计算给定二叉树的所有左叶子之和。
示例：
    3
   / \
  9  20
    /  \
   15   7
在这个二叉树中，有两个左叶子，分别是 9 和 15，所以返回 24
*/

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class problems_404 {
    public static void main(String[] arge) {
        TreeNode A1 = new TreeNode(1);
        TreeNode A2 = new TreeNode(2);
        TreeNode A3 = new TreeNode(2);
        TreeNode A4 = new TreeNode(3);
        TreeNode A5 = new TreeNode(4);
        TreeNode A6 = new TreeNode(4);
        TreeNode A7 = new TreeNode(3);
        A1.left = A2;
        A1.right = A3;
        A2.left = A4;
        A2.right = A5;
        A3.left = A6;
        A3.right = A7;
        int ret = new Solution().sumOfLeftLeaves(A1);
        System.out.println(ret);
    }
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
    static class Solution {
        int sum = 0;
        public int sumOfLeftLeaves(TreeNode root) {
            findNode(root, null);
            return sum;
        }
        private void findNode(TreeNode node, TreeNode parent){
            if(null == node) return;
            if(null == node.left && null == node.right && null != parent && parent.left == node) sum += node.val;

            if(null != node.left) findNode(node.left, node);
            if(null != node.right) findNode(node.right, node);
        }
    }
}